package main

import (
	common "git.mashibing.com/msb_47094/shopping-comm"
	consul "github.com/asim/go-micro/plugins/registry/consul/v4"
	opentracing2 "github.com/go-micro/plugins/v4/wrapper/trace/opentracing"
	"github.com/opentracing/opentracing-go"
	"go-micro.dev/v4"
	"go-micro.dev/v4/registry"
	"goproduct/domain/repository"
	"goproduct/domain/service"
	"goproduct/handler"
	"goproduct/proto"
	"log"
	"time"
)

func main() {
	//0 配置中心
	consulConfig, err := common.GetConsulConfig(common.ConsulStr, common.ProductFileKey)
	if err != nil {
		log.Println("consulConfig err :", err)
	}
	// 1.consul注册中心
	consulReist := consul.NewRegistry(func(options *registry.Options) {
		options.Addrs = []string{common.ConsulReistStr}
	})
	//链路追踪实列化  注意addr是 jaeper地址 端口号6831
	t, io, err := common.NewTracer("shop-product", common.ConsulIp+":6831")
	if err != nil {
		log.Fatal(err)
	}
	defer io.Close()
	//设置全局的Tracing
	opentracing.SetGlobalTracer(t)

	repcService := micro.NewService(
		micro.RegisterTTL(time.Second*30),
		micro.RegisterInterval(time.Second*30),
		micro.Name("shop-product"),
		micro.Address(":8082"),
		micro.Version("v1"),
		micro.Registry(consulReist),
		//引包
		micro.WrapHandler(opentracing2.NewHandlerWrapper(opentracing.GlobalTracer())),
	)
	//2.初始化db
	db, _ := common.GetMysqlFromConsul(consulConfig)
	//3.创建服务实例
	productDataService := service.NewProductDataService(repository.NewProductRepository(db))
	//4.注册handler
	proto.RegisterPageHandler(repcService.Server(), &handler.ProductHandler{productDataService})
	proto.RegisterShowProductDetailHandler(repcService.Server(), &handler.ProductHandler{productDataService})
	proto.RegisterShowProductSkuHandler(repcService.Server(), &handler.ProductHandler{productDataService})
	proto.RegisterShowDetailSkuHandler(repcService.Server(), &handler.ProductHandler{productDataService})
	proto.RegisterUpdateSkuHandler(repcService.Server(), &handler.ProductHandler{productDataService})
	//5.启动服务
	if err := repcService.Run(); err != nil {
		log.Println("start user service err :", err)
	}

}
